#pragma once

class ShaderProgram {
protected:

	GLuint program ;
	
	GLuint vertexShader;
	
	GLuint fragmentShader;
	
public:

	void use();

	ShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource);	
	
	~ShaderProgram();
	
};

void ShaderProgram::use(){
	glUseProgram(program);
}

ShaderProgram::ShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource){
	program = glCreateProgram();
	
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glAttachShader(program,vertexShader);
	glShaderSource(vertexShader,1,&vertexShaderSource,0);
	glCompileShader(vertexShader);
	
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glAttachShader(program,fragmentShader);
	glShaderSource(fragmentShader,1,&fragmentShaderSource,0);
	glCompileShader(fragmentShader);
	
	glLinkProgram(program);
	GLint linkStatus;
	glGetProgramiv(program,GL_LINK_STATUS,&linkStatus);
	if (linkStatus == GL_FALSE) {
		GLint infoLogLength;
		glGetProgramiv(program,GL_INFO_LOG_LENGTH,&infoLogLength);
		char* buffer = (char*) malloc (infoLogLength+1);
		glGetProgramInfoLog(program,infoLogLength,0,buffer);
		buffer[infoLogLength] = 0;
		printf("%s\n",buffer);
		free(buffer);
		exit(1);
	}
}

ShaderProgram::~ShaderProgram(){
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);
	glDeleteProgram(program);
}

